﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.Calibration.CApCalibrationSet
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.Threading;
using System.Xml;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.Calibration;

public class CApCalibrationSet : INotifyPropertyChanged
{
  private static Dictionary<string, List<string>> _MissingLocations = new Dictionary<string, List<string>>();
  private static Dictionary<string, List<string>> _MissingTargetObjects = new Dictionary<string, List<string>>();
  private static Dictionary<string, string> _MissingDevices = new Dictionary<string, string>();
  private string _Id;
  private TxObjectList _TargetObjectList = new TxObjectList();
  private int _AppliedOrder;
  private bool _IsApplied;
  private bool _IsApplicable;
  private string _Name;
  private string _DeviceName = "";
  private DateTime _LastModified;
  private string _Description;
  private bool _IsMaintainOrientation;
  private TxFrame _RelativeToFrame;
  private TxTransformation _RelativeToTransformation;
  private CApCalibrationTransformation _CalibrationTransform = new CApCalibrationTransformation();
  private ITxObject _Device;

  public event PropertyChangedEventHandler PropertyChanged;

  public static Dictionary<string, List<string>> MissingLocations
  {
    get => CApCalibrationSet._MissingLocations;
  }

  public static Dictionary<string, List<string>> MissingTargetObjects
  {
    get => CApCalibrationSet._MissingTargetObjects;
  }

  public static Dictionary<string, string> MissingDevices => CApCalibrationSet._MissingDevices;

  public string Id
  {
    get => this._Id;
    set
    {
      this._Id = value;
      if (this.PropertyChanged == null)
        return;
      this.PropertyChanged((object) this, new PropertyChangedEventArgs(nameof (Id)));
    }
  }

  public TxObjectList TargetObjectList
  {
    get => this._TargetObjectList;
    set
    {
      this._TargetObjectList = value;
      if (this.PropertyChanged == null)
        return;
      this.PropertyChanged((object) this, new PropertyChangedEventArgs(nameof (TargetObjectList)));
    }
  }

  public int AppliedOrder
  {
    get => this._AppliedOrder;
    set
    {
      this._AppliedOrder = value;
      if (this.PropertyChanged != null)
        this.PropertyChanged((object) this, new PropertyChangedEventArgs(nameof (AppliedOrder)));
      this.IsApplied = this._AppliedOrder > 0;
    }
  }

  public bool IsApplied
  {
    get => this._IsApplied;
    set
    {
      this._IsApplied = value;
      if (this.PropertyChanged == null)
        return;
      this.PropertyChanged((object) this, new PropertyChangedEventArgs(nameof (IsApplied)));
    }
  }

  public bool IsApplicable
  {
    get => this._IsApplicable;
    set
    {
      this._IsApplicable = value;
      if (this.PropertyChanged == null)
        return;
      this.PropertyChanged((object) this, new PropertyChangedEventArgs(nameof (IsApplicable)));
    }
  }

  public string Name
  {
    get => this._Name;
    set
    {
      this._Name = value;
      if (this.PropertyChanged == null)
        return;
      this.PropertyChanged((object) this, new PropertyChangedEventArgs(nameof (Name)));
    }
  }

  public string DeviceName
  {
    get => this._DeviceName;
    set
    {
      this._DeviceName = value;
      if (this.PropertyChanged == null)
        return;
      this.PropertyChanged((object) this, new PropertyChangedEventArgs(nameof (DeviceName)));
    }
  }

  public DateTime LastModified
  {
    get => this._LastModified;
    set
    {
      this._LastModified = value;
      if (this.PropertyChanged == null)
        return;
      this.PropertyChanged((object) this, new PropertyChangedEventArgs(nameof (LastModified)));
    }
  }

  public string Description
  {
    get => this._Description;
    set
    {
      this._Description = value;
      if (this.PropertyChanged == null)
        return;
      this.PropertyChanged((object) this, new PropertyChangedEventArgs(nameof (Description)));
    }
  }

  public bool IsMaintainOrientation
  {
    get => this._IsMaintainOrientation;
    set
    {
      this._IsMaintainOrientation = value;
      this.CalibrationTransform.IsMaintainOrientation = this._IsMaintainOrientation;
      this.CheckIsApplicable();
    }
  }

  public TxFrame RelativeToFrame
  {
    get => this._RelativeToFrame;
    set
    {
      this._RelativeToFrame = value;
      if (this.PropertyChanged != null)
        this.PropertyChanged((object) this, new PropertyChangedEventArgs(nameof (RelativeToFrame)));
      this.CalibrationTransform.RelativeToFrame = this._RelativeToFrame;
      if (TxFrame.op_Inequality(this._RelativeToFrame, (ITxObject) null))
      {
        this.RelativeToTransformation = this.RelativeToFrame.AbsoluteLocation;
      }
      else
      {
        if (!TxTransformation.op_Equality(this.RelativeToTransformation, (TxTransformation) null))
          return;
        this.RelativeToTransformation = new TxTransformation();
      }
    }
  }

  public TxTransformation RelativeToTransformation
  {
    get => this._RelativeToTransformation;
    set
    {
      this._RelativeToTransformation = value;
      if (this.PropertyChanged != null)
        this.PropertyChanged((object) this, new PropertyChangedEventArgs(nameof (RelativeToTransformation)));
      this.CalibrationTransform.RelativeToTransformation = this._RelativeToTransformation;
      if (!TxFrame.op_Inequality(this.RelativeToFrame, (ITxObject) null) || !TxTransformation.op_Inequality(this.RelativeToFrame.AbsoluteLocation, this._RelativeToTransformation))
        return;
      this.RelativeToFrame = (TxFrame) null;
    }
  }

  public CApCalibrationTransformation CalibrationTransform
  {
    get => this._CalibrationTransform;
    set
    {
      this._CalibrationTransform = value;
      if (this.PropertyChanged == null)
        return;
      this.PropertyChanged((object) this, new PropertyChangedEventArgs(nameof (CalibrationTransform)));
    }
  }

  public ITxObject Device
  {
    get => this._Device;
    set
    {
      this._Device = value;
      if (this.PropertyChanged != null)
        this.PropertyChanged((object) this, new PropertyChangedEventArgs(nameof (Device)));
      if (this._Device != null)
        this.DeviceName = this._Device.Name;
      else
        this.DeviceName = "";
    }
  }

  public CApCalibrationSet() => this.Id = Guid.NewGuid().ToString();

  public void Clone(CApCalibrationSet origin)
  {
    this.AppliedOrder = origin.AppliedOrder;
    this.CalibrationTransform.AutoCalibrate = false;
    this.CalibrationTransform.Clone(origin.CalibrationTransform);
    this.Description = origin.Description;
    this.Device = origin.Device;
    this.DeviceName = origin.DeviceName;
    this.LastModified = origin.LastModified;
    this.Name = origin.Name;
    this.IsMaintainOrientation = origin.IsMaintainOrientation;
    this.RelativeToFrame = origin.RelativeToFrame;
    this.RelativeToTransformation = origin.RelativeToTransformation;
    this.TargetObjectList = new TxObjectList(origin.TargetObjectList);
  }

  public void AddCalibrationPair(CApCalibrationPair cp)
  {
    this.CalibrationTransform.RelativeToFrame = this.RelativeToFrame;
    this.CalibrationTransform.RelativeToTransformation = this.RelativeToTransformation;
    this.CalibrationTransform.IsMaintainOrientation = this.IsMaintainOrientation;
    this.CalibrationTransform.AddPair(cp);
  }

  public void RemoveCalibrationPair(CApCalibrationPair cp)
  {
    this.CalibrationTransform.RemovePair(cp);
  }

  public void ClearCalibrationPairs() => this.CalibrationTransform.ClearPairs();

  public void AddTargetObject(ITxObject targetObject)
  {
    if (((Collection<ITxObject>) this.TargetObjectList).Contains(targetObject))
      return;
    ((Collection<ITxObject>) this.TargetObjectList).Add(targetObject);
  }

  public void RemoveTargetObject(ITxObject targetObject)
  {
    ((Collection<ITxObject>) this.TargetObjectList).Remove(targetObject);
  }

  public bool CheckIsApplicable()
  {
    bool flag = !string.IsNullOrEmpty(this.Id) && this.AppliedOrder == 0 && !this.IsApplied && !string.IsNullOrEmpty(this.Name) && this.Device != null && !string.IsNullOrEmpty(this.DeviceName) && TxTransformation.op_Inequality(this.RelativeToTransformation, (TxTransformation) null) && ((Collection<ITxObject>) this.TargetObjectList).Count > 0 && !this.CalibrationTransform.MaintainTransformation.Equals((object) new TxTransformation());
    this.IsApplicable = flag;
    return flag;
  }

  public bool Apply(int order)
  {
    bool flag = true;
    if (this.AppliedOrder == 0)
    {
      TxObjectList locatableList = new TxObjectList();
      this.GetLocatableDescendants(this.TargetObjectList, locatableList);
      TxLocatableObjectEx.SetObjectListLocationOffset(locatableList, this.RelativeToTransformation, this.CalibrationTransform.MaintainTransformation);
      TxApplication.ActiveUndoManager.ClearAllTransactions();
      this.AppliedOrder = order;
      this.IsApplied = true;
    }
    else
      flag = false;
    return flag;
  }

  public bool Unapply()
  {
    bool flag = true;
    if (this.AppliedOrder > 0)
    {
      TxObjectList locatableList = new TxObjectList();
      this.GetLocatableDescendants(this.TargetObjectList, locatableList);
      TxLocatableObjectEx.SetObjectListLocationOffset(locatableList, this.RelativeToTransformation, this.CalibrationTransform.MaintainTransformation.Inverse);
      TxApplication.ActiveUndoManager.ClearAllTransactions();
      this.AppliedOrder = 0;
      this.IsApplied = false;
      this.CheckIsApplicable();
    }
    else
      flag = false;
    return flag;
  }

  private bool GetLocatableDescendants(TxObjectList objList, TxObjectList locatableList)
  {
    TxObjectList txObjectList = new TxObjectList();
    foreach (ITxObject itxObject in (Collection<ITxObject>) objList)
    {
      switch (itxObject)
      {
        case ITxLocatableObject _:
          ((Collection<ITxObject>) txObjectList).Add(itxObject);
          continue;
        case ITxObjectCollection _:
          TxObjectList allDescendants = (itxObject as ITxObjectCollection).GetAllDescendants((ITxTypeFilter) new TxTypeFilter(typeof (ITxLocatableObject)));
          if (((Collection<ITxObject>) allDescendants).Count > 0)
          {
            ((TxObjectList<ITxObject>) txObjectList).AddRange((TxObjectList<ITxObject>) allDescendants);
            continue;
          }
          continue;
        default:
          return false;
      }
    }
    ((TxObjectList<ITxObject>) locatableList).AddRange((TxObjectList<ITxObject>) txObjectList);
    return true;
  }

  public XmlDocument ToXml()
  {
    CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
    Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
    XmlDocument xDoc = new XmlDocument();
    XmlElement root = this.WriteRootElement(xDoc);
    this.WriteNameElement(xDoc, root);
    this.WriteDeviceElement(xDoc, root);
    this.WriteFrameElement(xDoc, root);
    this.WriteDescriptionElement(xDoc, root);
    this.WriteLastModifiedElement(xDoc, root);
    this.WriteIsMaintainOrientationElement(xDoc, root);
    this.WriteIsAppliedElement(xDoc, root);
    this.WriteAppliedOrderElement(xDoc, root);
    this.WriteCalibrationPairsElement(xDoc, root);
    this.WriteTargetObjectsElement(xDoc, root);
    this.WriteTranslationOnlyElement(xDoc, root);
    this.WriteTransformationElement(xDoc, root);
    this.WriteTransformationExceptionElement(xDoc, root);
    Thread.CurrentThread.CurrentCulture = currentCulture;
    return xDoc;
  }

  private void WriteTransformationElement(XmlDocument xDoc, XmlElement root)
  {
    CApCalibrationSetsXmlManager.AddElement(xDoc, root, "transformation").InnerText = this._CalibrationTransform.Transformation.ToString((TxTransformation.TxTransformationStyle) 2, 12);
  }

  private void WriteTransformationExceptionElement(XmlDocument xDoc, XmlElement root)
  {
    CApCalibrationSetsXmlManager.AddElement(xDoc, root, "transformationException").InnerText = ((int) this._CalibrationTransform.CalibrationException).ToString();
  }

  private void WriteTranslationOnlyElement(XmlDocument xDoc, XmlElement root)
  {
    CApCalibrationSetsXmlManager.AddElement(xDoc, root, "translationOnly").InnerText = this._CalibrationTransform.TranslationOnly.ToString();
  }

  private void WriteTargetObjectsElement(XmlDocument xDoc, XmlElement root)
  {
    XmlElement parent = CApCalibrationSetsXmlManager.AddElement(xDoc, root, "targetObjects");
    foreach (ITxObject targetObject in (Collection<ITxObject>) this.TargetObjectList)
    {
      XmlElement xmlElement = CApCalibrationSetsXmlManager.AddElement(xDoc, parent, "targetObject");
      CApCalibrationSetsXmlManager.AddElement(xDoc, xmlElement, "name").InnerText = targetObject.Name;
      string id = CApCalibrationSetsXmlManager.GetID(targetObject);
      CApCalibrationSetsXmlManager.SetAttributeToElement(xDoc, xmlElement, "global-id", id);
    }
  }

  private XmlElement WriteRootElement(XmlDocument xDoc)
  {
    XmlElement element = xDoc.CreateElement("calibrationSet");
    xDoc.AppendChild((XmlNode) element);
    CApCalibrationSetsXmlManager.SetAttributeToElement(xDoc, element, "ID", this._Id);
    return element;
  }

  private void WriteCalibrationPairsElement(XmlDocument xDoc, XmlElement root)
  {
    XmlElement parent1 = CApCalibrationSetsXmlManager.AddElement(xDoc, root, "calibrationPairs");
    foreach (CApCalibrationPair calibrationPair in (Collection<CApCalibrationPair>) this.CalibrationTransform.CalibrationPairs)
    {
      XmlElement parent2 = CApCalibrationSetsXmlManager.AddElement(xDoc, parent1, "calibrationPair");
      XmlElement xmlElement1 = CApCalibrationSetsXmlManager.AddElement(xDoc, parent2, "virtualPoint");
      string id1 = CApCalibrationSetsXmlManager.GetID((ITxObject) calibrationPair.VirtualPoint);
      CApCalibrationSetsXmlManager.SetAttributeToElement(xDoc, xmlElement1, "global-id", id1);
      this.WriteLocationElement(xDoc, xmlElement1, calibrationPair.VirtualPoint, calibrationPair.VirtualLocation);
      XmlElement xmlElement2 = CApCalibrationSetsXmlManager.AddElement(xDoc, parent2, "actualPoint");
      string id2 = CApCalibrationSetsXmlManager.GetID((ITxObject) calibrationPair.ActualPoint);
      CApCalibrationSetsXmlManager.SetAttributeToElement(xDoc, xmlElement2, "global-id", id2);
      this.WriteLocationElement(xDoc, xmlElement2, calibrationPair.ActualPoint, calibrationPair.ActualLocation);
      CApCalibrationSetsXmlManager.AddElement(xDoc, parent2, "isActive").InnerText = calibrationPair.IsActive.ToString();
    }
  }

  private void WriteAppliedOrderElement(XmlDocument xDoc, XmlElement root)
  {
    CApCalibrationSetsXmlManager.AddElement(xDoc, root, "appliedOrder").InnerText = this._AppliedOrder.ToString();
  }

  private void WriteIsAppliedElement(XmlDocument xDoc, XmlElement root)
  {
    CApCalibrationSetsXmlManager.AddElement(xDoc, root, "isApplied").InnerText = this.IsApplied.ToString();
  }

  private void WriteIsMaintainOrientationElement(XmlDocument xDoc, XmlElement root)
  {
    CApCalibrationSetsXmlManager.AddElement(xDoc, root, "isMaintainOrientation").InnerText = this.IsMaintainOrientation.ToString();
  }

  private void WriteLastModifiedElement(XmlDocument xDoc, XmlElement root)
  {
    CApCalibrationSetsXmlManager.AddElement(xDoc, root, "lastModified").InnerText = this._LastModified.ToString();
  }

  private void WriteDescriptionElement(XmlDocument xDoc, XmlElement root)
  {
    CApCalibrationSetsXmlManager.AddElement(xDoc, root, "description").InnerText = this._Description;
  }

  private void WriteFrameElement(XmlDocument xDoc, XmlElement root)
  {
    if (TxFrame.op_Inequality(this.RelativeToFrame, (ITxObject) null))
    {
      XmlElement xmlElement = CApCalibrationSetsXmlManager.AddElement(xDoc, root, "frame");
      string id = CApCalibrationSetsXmlManager.GetID((ITxObject) this.RelativeToFrame);
      CApCalibrationSetsXmlManager.SetAttributeToElement(xDoc, xmlElement, "global-id", id);
      this.WriteLocationElement(xDoc, xmlElement, (ITxLocatableObject) this.RelativeToFrame, this.RelativeToTransformation);
    }
    else
    {
      if (!TxTransformation.op_Inequality(this.RelativeToTransformation, (TxTransformation) null))
        return;
      XmlElement parent = CApCalibrationSetsXmlManager.AddElement(xDoc, root, "frame");
      this.WriteLocationElement(xDoc, parent, this.RelativeToTransformation);
    }
  }

  private void WriteLocationElement(
    XmlDocument xDoc,
    XmlElement parent,
    ITxLocatableObject locObj,
    TxTransformation locTrans)
  {
    if (locObj == null)
      return;
    XmlElement parent1 = CApCalibrationSetsXmlManager.AddElement(xDoc, parent, "location");
    CApCalibrationSetsXmlManager.AddElement(xDoc, parent1, "name").InnerText = ((ITxObject) locObj).Name;
    XmlElement element = CApCalibrationSetsXmlManager.AddElement(xDoc, parent1, "pos");
    CApCalibrationSetsXmlManager.SetAttributeToElement(xDoc, element, "x", locTrans.Translation.X.ToString());
    CApCalibrationSetsXmlManager.SetAttributeToElement(xDoc, element, "y", locTrans.Translation.Y.ToString());
    CApCalibrationSetsXmlManager.SetAttributeToElement(xDoc, element, "z", locTrans.Translation.Z.ToString());
    CApCalibrationSetsXmlManager.SetAttributeToElement(xDoc, element, "rx", locTrans.RotationRPY_XYZ.X.ToString());
    CApCalibrationSetsXmlManager.SetAttributeToElement(xDoc, element, "ry", locTrans.RotationRPY_XYZ.Y.ToString());
    CApCalibrationSetsXmlManager.SetAttributeToElement(xDoc, element, "rz", locTrans.RotationRPY_XYZ.Z.ToString());
  }

  private void WriteLocationElement(XmlDocument xDoc, XmlElement parent, TxTransformation locObj)
  {
    if (!TxTransformation.op_Inequality(locObj, (TxTransformation) null))
      return;
    XmlElement parent1 = CApCalibrationSetsXmlManager.AddElement(xDoc, parent, "location");
    XmlElement element = CApCalibrationSetsXmlManager.AddElement(xDoc, parent1, "pos");
    CApCalibrationSetsXmlManager.SetAttributeToElement(xDoc, element, "x", locObj.Translation.X.ToString());
    CApCalibrationSetsXmlManager.SetAttributeToElement(xDoc, element, "y", locObj.Translation.Y.ToString());
    CApCalibrationSetsXmlManager.SetAttributeToElement(xDoc, element, "z", locObj.Translation.Z.ToString());
    CApCalibrationSetsXmlManager.SetAttributeToElement(xDoc, element, "rx", locObj.RotationRPY_XYZ.X.ToString());
    CApCalibrationSetsXmlManager.SetAttributeToElement(xDoc, element, "ry", locObj.RotationRPY_XYZ.Y.ToString());
    CApCalibrationSetsXmlManager.SetAttributeToElement(xDoc, element, "rz", locObj.RotationRPY_XYZ.Z.ToString());
  }

  private void WriteDeviceElement(XmlDocument xDoc, XmlElement root)
  {
    XmlElement xmlElement = CApCalibrationSetsXmlManager.AddElement(xDoc, root, "device");
    CApCalibrationSetsXmlManager.AddElement(xDoc, xmlElement, "name").InnerText = this._DeviceName;
    string id = CApCalibrationSetsXmlManager.GetID(this._Device);
    CApCalibrationSetsXmlManager.SetAttributeToElement(xDoc, xmlElement, "global-id", id);
  }

  private void WriteNameElement(XmlDocument xDoc, XmlElement root)
  {
    CApCalibrationSetsXmlManager.AddElement(xDoc, root, "name").InnerText = this._Name;
  }

  public static bool CreateFromXml(XmlNode node, ref CApCalibrationSet newSet)
  {
    bool flag1 = true;
    CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
    Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
    CApCalibrationSet resultSet = new CApCalibrationSet();
    CApCalibrationSet._MissingDevices.Clear();
    CApCalibrationSet._MissingLocations.Clear();
    CApCalibrationSet._MissingTargetObjects.Clear();
    bool flag2 = CApCalibrationSet.LoadId(node, resultSet) & flag1;
    bool flag3 = CApCalibrationSet.LoadName(node, resultSet) & flag2;
    bool flag4 = CApCalibrationSet.LoadDevice(node, resultSet) & flag3;
    bool flag5 = CApCalibrationSet.LoadFrame(node, resultSet) & flag4;
    bool flag6 = CApCalibrationSet.LoadDescription(node, resultSet) & flag5;
    bool flag7 = CApCalibrationSet.LoadLastModified(node, resultSet) & flag6;
    bool flag8 = CApCalibrationSet.LoadIsApplied(node, resultSet) & flag7;
    bool flag9 = CApCalibrationSet.LoadAppliedOrder(node, resultSet) & flag8;
    bool flag10 = CApCalibrationSet.LoadCalibrationPairs(node, resultSet) & flag9;
    bool flag11 = CApCalibrationSet.LoadTargetObjects(node, resultSet) & flag10;
    bool fromXml = CApCalibrationSet.LoadTransformation(node, resultSet) & flag11;
    CApCalibrationSet.LoadTransformationException(node, resultSet);
    CApCalibrationSet.LoadTranslationOnly(node, resultSet);
    CApCalibrationSet.LoadIsMaintainOrientation(node, resultSet);
    resultSet._CalibrationTransform.UpdateCalibrationResult();
    Thread.CurrentThread.CurrentCulture = currentCulture;
    newSet = resultSet;
    return fromXml;
  }

  private static bool LoadTransformationException(XmlNode node, CApCalibrationSet resultSet)
  {
    int intValue1 = -1;
    bool intValue2 = CApCalibrationSetsXmlManager.GetIntValue(node, "transformationException", ref intValue1);
    resultSet._CalibrationTransform.CalibrationException = (TxCalibrationTransformationException) intValue1;
    return intValue2;
  }

  private static bool LoadTransformation(XmlNode node, CApCalibrationSet resultSet)
  {
    bool flag = false;
    XmlNode node1 = node.SelectSingleNode("./transformation");
    TxTransformation txTransformation;
    if (node1 != null && TxTransformation.TryParse(CApCalibrationSetsXmlManager.GetXmlNodeText(node1), (TxTransformation.TxTransformationStyle) 2, ref txTransformation))
    {
      resultSet._CalibrationTransform.Transformation = txTransformation;
      flag = true;
    }
    return flag;
  }

  private static bool LoadTranslationOnly(XmlNode node, CApCalibrationSet resultSet)
  {
    bool flag = false;
    XmlNode node1 = node.SelectSingleNode("./translationOnly");
    TxVector txVector;
    if (node1 != null && TxVector.TryParse(CApCalibrationSetsXmlManager.GetXmlNodeText(node1), ref txVector))
    {
      resultSet._CalibrationTransform.TranslationOnly = txVector;
      flag = true;
    }
    if (!flag)
    {
      resultSet._CalibrationTransform.TranslationOnly = resultSet._CalibrationTransform.CalculateTranslation();
      flag = true;
    }
    return flag;
  }

  private static bool LoadTargetObjects(XmlNode node, CApCalibrationSet resultSet)
  {
    bool flag1 = true;
    XmlNodeList xmlNodeList = node.SelectNodes("./targetObjects/targetObject");
    foreach (XmlNode node1 in xmlNodeList)
    {
      bool flag2 = true;
      string xmlNodeAttribute = CApCalibrationSetsXmlManager.GetXmlNodeAttribute(node1, "global-id");
      if (xmlNodeAttribute != null && xmlNodeAttribute != string.Empty)
      {
        ITxObject targetObject = CApCalibrationSetsXmlManager.GetObject(xmlNodeAttribute);
        if (targetObject != null)
        {
          resultSet.AddTargetObject(targetObject);
        }
        else
        {
          flag2 = false;
          if (!CApCalibrationSet._MissingTargetObjects.ContainsKey(resultSet._Id))
            CApCalibrationSet._MissingTargetObjects.Add(resultSet._Id, new List<string>());
          XmlNode node2 = node1.SelectSingleNode("./name");
          if (node2 != null)
          {
            string xmlNodeText = CApCalibrationSetsXmlManager.GetXmlNodeText(node2);
            CApCalibrationSet._MissingTargetObjects[resultSet._Id].Add(xmlNodeText);
          }
          else
            CApCalibrationSet._MissingTargetObjects[resultSet._Id].Add(xmlNodeAttribute);
        }
      }
      flag1 &= flag2;
    }
    if (xmlNodeList.Count == 0)
      flag1 = true;
    return flag1;
  }

  private static bool LoadCalibrationPairs(XmlNode node, CApCalibrationSet resultSet)
  {
    bool flag1 = true;
    foreach (XmlNode selectNode in node.SelectNodes("./calibrationPairs/calibrationPair"))
    {
      bool flag2 = true;
      string pointId1 = "";
      string pointLocName1 = "";
      TxTransformation pointLocation1 = new TxTransformation();
      bool flag3 = flag2 && CApCalibrationSetsXmlManager.GetPointIdAndPos(selectNode, "virtualPoint", ref pointId1, ref pointLocName1, ref pointLocation1);
      string pointId2 = "";
      string pointLocName2 = "";
      TxTransformation pointLocation2 = new TxTransformation();
      bool flag4 = flag3 && CApCalibrationSetsXmlManager.GetPointIdAndPos(selectNode, "actualPoint", ref pointId2, ref pointLocName2, ref pointLocation2);
      bool boolValue = false;
      bool flag5 = flag4 && CApCalibrationSetsXmlManager.GetBoolValue(selectNode, "isActive", ref boolValue);
      if (flag5)
      {
        CApCalibrationPair cp = new CApCalibrationPair();
        ITxObject itxObject1 = CApCalibrationSetsXmlManager.GetObject(pointId1);
        if (itxObject1 != null)
        {
          if (itxObject1 is ITxLocatableObject itxLocatableObject1)
          {
            cp.VirtualPoint = itxLocatableObject1;
            cp.VirtualLocation = !pointLocation1.Equals((object) new TxTransformation()) ? pointLocation1 : itxLocatableObject1.AbsoluteLocation;
          }
        }
        else
        {
          flag5 = false;
          if (!CApCalibrationSet._MissingLocations.ContainsKey(resultSet._Id))
            CApCalibrationSet._MissingLocations.Add(resultSet._Id, new List<string>());
          if (!pointLocName1.Equals(""))
            CApCalibrationSet._MissingLocations[resultSet._Id].Add(pointLocName1);
          else
            CApCalibrationSet._MissingLocations[resultSet._Id].Add(pointId1);
        }
        if (flag5)
        {
          ITxObject itxObject2 = CApCalibrationSetsXmlManager.GetObject(pointId2);
          if (itxObject2 != null)
          {
            if (itxObject2 is ITxLocatableObject itxLocatableObject2)
            {
              cp.ActualPoint = itxLocatableObject2;
              cp.ActualLocation = !pointLocation2.Equals((object) new TxTransformation()) ? pointLocation2 : itxLocatableObject2.AbsoluteLocation;
            }
          }
          else
          {
            flag5 = false;
            if (!CApCalibrationSet._MissingLocations.ContainsKey(resultSet._Id))
              CApCalibrationSet._MissingLocations.Add(resultSet._Id, new List<string>());
            if (!pointLocName2.Equals(""))
              CApCalibrationSet._MissingLocations[resultSet._Id].Add(pointLocName2);
            else
              CApCalibrationSet._MissingLocations[resultSet._Id].Add(pointId2);
          }
          if (flag5)
          {
            cp.IsActive = boolValue;
            resultSet.AddCalibrationPair(cp);
          }
        }
      }
      flag1 &= flag5;
    }
    return flag1;
  }

  private static bool LoadAppliedOrder(XmlNode node, CApCalibrationSet resultSet)
  {
    int intValue1 = 0;
    bool intValue2 = CApCalibrationSetsXmlManager.GetIntValue(node, "appliedOrder", ref intValue1);
    resultSet._AppliedOrder = intValue1;
    return intValue2;
  }

  private static bool LoadIsMaintainOrientation(XmlNode node, CApCalibrationSet resultSet)
  {
    bool boolValue1 = false;
    bool boolValue2 = CApCalibrationSetsXmlManager.GetBoolValue(node, "isMaintainOrientation", ref boolValue1);
    resultSet.IsMaintainOrientation = boolValue1;
    return boolValue2;
  }

  private static bool LoadIsApplied(XmlNode node, CApCalibrationSet resultSet)
  {
    bool boolValue1 = false;
    bool boolValue2 = CApCalibrationSetsXmlManager.GetBoolValue(node, "isApplied", ref boolValue1);
    resultSet._IsApplied = boolValue1;
    return boolValue2;
  }

  private static bool LoadLastModified(XmlNode node, CApCalibrationSet resultSet)
  {
    bool flag = false;
    XmlNode xmlNodeByName = CApCalibrationSetsXmlManager.GetXMLNodeByName(node, "lastModified");
    if (xmlNodeByName != null)
    {
      string xmlNodeText = CApCalibrationSetsXmlManager.GetXmlNodeText(xmlNodeByName);
      DateTime result = new DateTime();
      if (DateTime.TryParse(xmlNodeText, out result))
      {
        resultSet._LastModified = result;
        flag = true;
      }
    }
    return flag;
  }

  private static bool LoadDescription(XmlNode node, CApCalibrationSet resultSet)
  {
    bool flag = false;
    XmlNode xmlNodeByName = CApCalibrationSetsXmlManager.GetXMLNodeByName(node, "description");
    if (xmlNodeByName != null)
    {
      string xmlNodeText = CApCalibrationSetsXmlManager.GetXmlNodeText(xmlNodeByName);
      resultSet._Description = xmlNodeText;
      flag = true;
    }
    return flag;
  }

  private static bool LoadFrame(XmlNode node, CApCalibrationSet resultSet)
  {
    bool flag = false;
    XmlNode xmlNodeByName = CApCalibrationSetsXmlManager.GetXMLNodeByName(node, "frame");
    if (xmlNodeByName != null)
    {
      resultSet._RelativeToFrame = (TxFrame) null;
      resultSet._RelativeToTransformation = TxApplication.ActiveDocument.WorkingFrame;
      flag = true;
      string xmlNodeAttribute = CApCalibrationSetsXmlManager.GetXmlNodeAttribute(xmlNodeByName, "global-id");
      if (xmlNodeAttribute != null && !xmlNodeAttribute.Equals(""))
      {
        ITxObject itxObject = CApCalibrationSetsXmlManager.GetObject(xmlNodeAttribute);
        if (itxObject != null)
        {
          TxFrame txFrame = itxObject as TxFrame;
          if (TxFrame.op_Inequality(txFrame, (ITxObject) null))
          {
            resultSet._RelativeToFrame = txFrame;
            resultSet._RelativeToTransformation = txFrame.AbsoluteLocation;
          }
        }
      }
      if (TxFrame.op_Equality(resultSet._RelativeToFrame, (ITxObject) null))
      {
        string pointLocPosX = "";
        string pointLocPosY = "";
        string pointLocPosZ = "";
        string pointLocRotX = "";
        string pointLocRotY = "";
        string pointLocRotZ = "";
        if (CApCalibrationSetsXmlManager.LoadPointLocPos(node, "frame", ref pointLocPosX, ref pointLocPosY, ref pointLocPosZ) && CApCalibrationSetsXmlManager.LoadPointLocRot(node, "frame", ref pointLocRotX, ref pointLocRotY, ref pointLocRotZ))
        {
          resultSet._RelativeToFrame = (TxFrame) null;
          double result1 = 0.0;
          double result2 = 0.0;
          double result3 = 0.0;
          double result4 = 0.0;
          double result5 = 0.0;
          double result6 = 0.0;
          if (double.TryParse(pointLocPosX, out result1) && double.TryParse(pointLocPosY, out result2) && double.TryParse(pointLocPosZ, out result3) && double.TryParse(pointLocRotX, out result4) && double.TryParse(pointLocRotY, out result5) && double.TryParse(pointLocRotZ, out result6))
            resultSet._RelativeToTransformation = new TxTransformation(new TxVector(result1, result2, result3), new TxVector(result4, result5, result6), (TxTransformation.TxRotationType) 1);
        }
      }
    }
    return flag;
  }

  private static bool LoadDevice(XmlNode node, CApCalibrationSet resultSet)
  {
    bool flag = false;
    XmlNode xmlNodeByName = CApCalibrationSetsXmlManager.GetXMLNodeByName(node, "device");
    if (xmlNodeByName != null)
    {
      string xmlNodeAttribute = CApCalibrationSetsXmlManager.GetXmlNodeAttribute(xmlNodeByName, "global-id");
      if (xmlNodeAttribute != null)
      {
        ITxObject itxObject = CApCalibrationSetsXmlManager.GetObject(xmlNodeAttribute);
        if (itxObject != null)
        {
          if (itxObject is ITxDevice itxDevice)
          {
            resultSet._Device = (ITxObject) itxDevice;
            resultSet._DeviceName = ((ITxObject) itxDevice).Name;
            flag = true;
          }
        }
        else if (!CApCalibrationSet._MissingDevices.ContainsKey(resultSet._Id))
        {
          XmlNode node1 = xmlNodeByName.SelectSingleNode("./name");
          if (node1 != null)
          {
            string xmlNodeText = CApCalibrationSetsXmlManager.GetXmlNodeText(node1);
            CApCalibrationSet._MissingDevices.Add(resultSet._Id, xmlNodeText);
          }
          else
            CApCalibrationSet._MissingDevices.Add(resultSet._Id, xmlNodeAttribute);
          flag = false;
        }
      }
      else
        flag = false;
    }
    return flag;
  }

  private static bool LoadName(XmlNode node, CApCalibrationSet resultSet)
  {
    bool flag = false;
    XmlNode xmlNodeByName = CApCalibrationSetsXmlManager.GetXMLNodeByName(node, "name");
    if (xmlNodeByName != null)
    {
      string xmlNodeText = CApCalibrationSetsXmlManager.GetXmlNodeText(xmlNodeByName);
      resultSet._Name = xmlNodeText;
      flag = true;
    }
    return flag;
  }

  private static bool LoadId(XmlNode node, CApCalibrationSet resultSet)
  {
    bool flag = false;
    string xmlNodeAttribute = CApCalibrationSetsXmlManager.GetXmlNodeAttribute(node, "ID");
    if (xmlNodeAttribute != null)
    {
      resultSet._Id = xmlNodeAttribute;
      flag = true;
    }
    return flag;
  }
}
